package org.simon.myauctioneer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.simon.myauctioneer.dto.User;
import org.simon.myauctioneer.service.ArticleService;

/**
 * Servlet implementation class ArticleServlet
 */
@WebServlet({ "/createArticle", "/showArticles", "/updateArticle",
		"/deleteArticle" })
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String servletPath = request.getServletPath();
		if (servletPath.equals("/showArticles")) {
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			ArticleService articleService = new ArticleService();
			request.setAttribute("aAL", articleService.loadArticles(user.getUserId()));
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("showArticles.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String servletPath = request.getServletPath();

		if (servletPath.equals("/createArticle")) {
			String brand, familyLine, model, condition, description, createdAt, validUntil, category, price, saleType, userId;

			long now = System.currentTimeMillis();
			Date date = new Date(now);
			SimpleDateFormat formatter = new SimpleDateFormat(
					"dd-MMM-yyyy HH:mm");
			createdAt = formatter.format(date);

			brand = request.getParameter("brand");
			familyLine = request.getParameter("familyLine");
			model = request.getParameter("model");
			condition = request.getParameter("condition");
			description = request.getParameter("description");
			validUntil = request.getParameter("validUntil");
			category = request.getParameter("category");
			price = request.getParameter("price");
			saleType = request.getParameter("saleType");
			userId = request.getParameter("userId");

			ArticleService articleService = new ArticleService();
			boolean result = articleService.createArticle(brand, familyLine,
					model, condition, description, createdAt, validUntil,
					category, price, saleType, userId);

			if (result) {
				request.setAttribute("title", "Article created");
				request.setAttribute("content", "The article is now created.");
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("success.jsp");
				dispatcher.forward(request, response);
			} else {
				response.sendRedirect("createArticle.jsp");
			}
		}
	}

}
